#include <iostream>
#include <string>
using std::string;
#include <map>
#include <algorithm>
#include "frameFactory.h"

FrameFactory::~FrameFactory() { 
  std::map<string, Frame*>::iterator pos = frames.begin(); 
  while ( pos != frames.end() ) {
    delete pos->second;
    ++pos;
  }
  frames.clear();
  std::cout<<"delete framefactory"<<std::endl;
}

Frame* FrameFactory::getFrame(const string& filename,
    Uint16 width, Uint16 height,
    Sint16 src_x, Sint16 src_y, bool setcolorkey) {
  Frame * spriteFrame;
  std::map<string, Frame*>::iterator pos = frames.begin(); 
  while ( pos != frames.end() ) {
    if ( pos->first == filename ) break;
    else ++pos;
  }
  if ( pos == frames.end() ) {
    spriteFrame = new Frame(filename,
                  width, height, src_x, src_y, setcolorkey);
    frames[filename] = spriteFrame;
  }
  else {
    spriteFrame = pos->second;
  }
  return spriteFrame;
}

Frame* FrameFactory::getFrame(SDL_Surface* spr,const string& filename, Uint16 width, Uint16 height,
      Sint16 src_x, Sint16 src_y, bool setcolorkey){
	Frame * spriteFrame;
  std::map<string, Frame*>::iterator pos = frames.begin(); 
  while ( pos != frames.end() ) {
    if ( pos->first == filename ) break;
    else ++pos;
  }
  if ( pos == frames.end() ) {
    spriteFrame = new Frame(spr,width, height, src_x, src_y, setcolorkey);
    frames[filename] = spriteFrame;
  }
  else {
    spriteFrame = pos->second;
  }
  return spriteFrame;
	  }
